package model

import (
	"trade/logger"
)

type Kline struct {
	ID   string `json:"id"`
	Data []struct {
		Vol    float64 `json:"vol"`
		Count  float64 `json:"count"`
		Open   float64 `json:"open"`
		Close  float64 `json:"close"`
		Low    float64 `json:"low"`
		High   float64 `json:"high"`
		Amount float64 `json:"amount"`
	}
}

func (this *Kline) CanOrder(price, flatPrice float64, direction string, index Index) bool {
	if len(this.Data) < 1 {
		return false
	}
	recommand := false
	cur := this.Data[0] //最近15分钟蜡烛线
	min := cur.Low
	max := cur.High
	if len(this.Data) > 1 {
		last := this.Data[1]
		if min > last.Low {
			min = last.Low
		}
		if max < last.High {
			max = last.High
		}
	}
	if direction == "buy" { //多单 平仓单 要小于15分钟最高点
		recommand = flatPrice < max && price-index.IndexPrice < 10
	} else { //空单 平仓单要大于15分钟最小值
		recommand = min < flatPrice && price-index.IndexPrice > -10
	}
	logger.Log.Debug(direction, " 最近15分钟最低值：$", min, " 最高值: $", max, "下单价: $", price, "盈利平仓价：$", flatPrice, "指数价", index.IndexPrice)
	return recommand
}
